package com.cypher.encryptionanddecryption.fragment.protocol.nb25;

import android.os.Bundle;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.cypher.encryptionanddecryption.R;
import com.cypher.encryptionanddecryption.bean.CommunicationBean;
import com.cypher.encryptionanddecryption.fragment.protocol.AbstractProtocolFragment;
import com.cypher.encryptionanddecryption.util.HexUtils;
import com.cypher.encryptionanddecryption.util.Nb25ProtocolUtils;

import org.apache.commons.lang3.StringUtils;

import java.util.Map;

/**
 * NB2.5只读界面
 * @author: 古霸天
 * @date: 2020/11/16
 */
public class ReadonlyFragment extends AbstractProtocolFragment {
    private CommunicationBean bean;
    private TextView commResult;
    public ReadonlyFragment(CommunicationBean bean) {
        this.bean = bean;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_nb25_readonly, container, false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        commResult = view.findViewById(R.id.comm_2_5_result);
    }

    @Override
    public void fillFragment(String data) {
        // 每次渲染时清空控件
        commResult.setText("");

        // 程序暂停0.2秒后开始渲染数据
        Runnable r = () -> {
            reader(data);
        };
        new Handler().postDelayed(r, 200);

    }

    @Override
    public String getHexCode(String rwType) {
        // 特殊情况处理
        if (StringUtils.equals("true", bean.getSpecial())) {

        }
        // 组包
        return Nb25ProtocolUtils.getReadCommand(bean.getHexCode(), PROTOCOL_VERSION);
    }

    /**
     * 渲染页面
     * @param data
     */
    private void reader(String data) {
        String formatData = Nb25ProtocolUtils.formatReceiveData(data);
        String result = "";

        final Map<String, Object> attr = bean.getAttr();

        final String type = !attr.containsKey("type") ? "" : attr.get("type").toString();
        switch (type) {
            case "number":
                int decimalDigits = !attr.containsKey("decimalDigits") ? 0 : (int) attr.get("decimalDigits");
                String unit = !attr.containsKey("unit") ? "" : attr.get("unit").toString();
                result = HexUtils.formatDouble(
                        HexUtils.integerToDouble(Integer.parseInt(formatData, 16), decimalDigits), decimalDigits);
                result += unit;
                break;
            case "code":
                result = formatData;
                break;
            case "plaintext":
                result = HexUtils.ascToStr(formatData);
                break;
            case "spinner":
                result = readerSpinner(attr, formatData);
                break;
            case "special":
                result = readerSpecialView(formatData);
                break;
        }

        commResult.setText(bean.getName() + "：" + result);
    }

    /**
     * 处理下拉框数据类型
     * @param data
     * @return
     */
    private String readerSpinner(final Map<String, Object> attr, String data) {

        final String[] codes = attr.containsKey("code") ? attr.get("code").toString().split(",") : new String[0];
        final String[] names = attr.containsKey("name") ? attr.get("name").toString().split(",") : new String[0];
        int index = HexUtils.getArrayIndex(codes, data);
        if (index != -1) {
            return names[index];
        }
        return "";
    }

    /**
     * 处理特殊的只读数据
     * @param data
     * @return
     */
    private String readerSpecialView(String data) {

        StringBuffer result = new StringBuffer();
        switch (bean.getHexCode()) {
            case "0011":
                result.append("RSRP ").append(Integer.parseInt(data.substring(0, 4), 16));
                result.append(" SNR ").append(Integer.parseInt(data.substring(4, 8), 16));
                break;
        }
        return result.toString();
    }
}
